package com.example.pdfexcelconverter.controller;

import com.example.pdfexcelconverter.dto.ExcelResponse;
import com.example.pdfexcelconverter.service.PdfToExcelService;
import com.example.pdfexcelconverter.service.PdfToJsonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/pdf/")
public class PdfController {

    @Autowired
    private PdfToExcelService pdfToExcelService;

    @Operation(summary = "将PDF文件转换为Excel文件")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "转换成功", content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")),
            @ApiResponse(responseCode = "400", description = "无效的文件"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })

    @PostMapping("/excel")
    public ResponseEntity<ByteArrayResource> convertPdfToExcel(
            @Parameter(description = "上传的PDF文件") @RequestParam("file") MultipartFile pdfFile) {
        try {
            byte[] excelBytes = pdfToExcelService.convertPdfToExcel(pdfFile.getBytes());
            ByteArrayResource resource = new ByteArrayResource(excelBytes);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=converted.xlsx")
                    .contentLength(excelBytes.length)
                    .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                    .body(resource);
        } catch (Exception e) {
            throw new RuntimeException("PDF转换失败", e);
        }
    }


    @Autowired
    private PdfToJsonService pdfToJsonService;
    @PostMapping("/json")
    public ResponseEntity<ByteArrayResource> convertPdfToJson(
            @Parameter(description = "上传的PDF文件") @RequestParam("file") MultipartFile pdfFile) {
        try {
            byte[] excelBytes = pdfToJsonService.convertPdfToJson(pdfFile.getBytes());
            ByteArrayResource resource = new ByteArrayResource(excelBytes);

            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=converted.xlsx")
                    .contentLength(excelBytes.length)
                    .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                    .body(resource);
        } catch (Exception e) {
            throw new RuntimeException("PDF转换失败", e);
        }
    }
}

